SpringCloud Gateway 的使用(一)
Spring Cloud Gateway 的使用
内容概览
一. Springboot
对应的 Springcloud
版本如下~
官网地址 :https://spring.io/projects/spring-cloud
各位大佬 请注意下~ ,最后一句英文还说 不再支持 Dalston
, Edgware
, Finchley
,
Greenwich
这四个版本啦
二. 使用
pom
文件
<properties>
<spring.cloud-version>Hoxton.SR8</spring.cloud-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
yaml
文件
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: http://localhost:8080/
predicates:
- Path=/gateway/a
filters:
# 去掉请求路径中的 第一个 如 /gateway/a 变成/a
- StripPrefix=1
# 重新设置这个路由地址
- SetPath=/debug
三. 组成
官方文档
Route
The basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates, and a collection of filters. A route is matched if the aggregate predicate is true.
对应该类(可对比上面 yaml
文件):
Predicate
This is a Java 8 Function Predicate. The input type is a Spring Framework
ServerWebExchange
. This lets you match on anything from the HTTP request, such as headers or parameters.
当匹配时会拦截该请求
Filter
These are instances of Spring Framework
GatewayFilter
that have been constructed with a specific factory. Here, you can modify requests and responses before or after sending the downstream request.
可以配置添加更多过滤器~
四. 工作原理
由于 gateway
包中 使用了 WEBFLUX
, 不能和这个 WEBMVC
同时存在,所以要移除 spring-boot-starter-web
包
工作原理图(来自官网):就一堆过滤器
五. Route Predicate Factories(★★★★★)
Route Predicate Factories:https://docs.spring.io/spring-cloud-gateway/docs/3.0.1/reference/html/#gateway-request-predicates-factories
可以看到这里有 11 种匹配模式~
匹配模式大全(重点!)
这里的匹配满足正则表达式
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
# 匹配在这个时间之后的
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
# 匹配在这个时间之后的
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
# 匹配这个时间中间的
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
# 匹配 cookie 为 chocolate=ch.p 时拦截该请求
- Cookie=chocolate, ch.p
# 当 header 中有 X-Request-Id 该请求头时,且它的值匹配该正则时 拦截
- Header=X-Request-Id, \d+
# 匹配该 host 地址~
- Host=**.somehost.org,**.anotherhost.org
# 匹配该 请求方法
- Method=GET,POST
# 匹配该路径
- Path=/gateway/a
# 匹配该请求参数~ 当请求参数为red 且值为 gree开头的~
- Query=red, gree.
# 匹配 网址为 192.168.1.1 - 192.168.1.254
- RemoteAddr=192.168.1.1/24
根据权重匹配
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: https://weighthigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightlow.org
predicates:
- Weight=group1, 2
六. Gateway Filter Factories(★★★★★)
Gateway Filter Factories : https://docs.spring.io/spring-cloud-gateway/docs/3.0.1/reference/html/#gatewayfilter-factories
数了下~ 官网居然多达 30 个 filter 给你使用
另外一篇博文中再写写~ , 这里可以先简单参考下 4ye 在上面的 yaml
文件中的配置
七. Global Filters(★★★★★)
Global Filters: https://docs.spring.io/spring-cloud-gateway/docs/3.0.1/reference/html/#global-filters
全局过滤器~😄
自定义过滤器
@Component
public class CustomGlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("custom global filter");
return chain.filter(exchange);
}
@Override
public int getOrder() {
return -1;
}
}
初五了,你好有才(财)!
谢谢可爱又帅气的大佬们的观看!祝您 天天开心!😄
感谢您的关注!您的每个关注,都是博主生发的动力 😝
点个“在看”表示朕
已阅
IDEA 远程调试篇(情人节之抽个小锦鲤~手慢无)
Springboot 居然可以设置动态的 banner !!(悄悄滴~)
合成大西瓜之抱歉~ 我只有大西瓜和刀剑神域~!!